using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;

using WHC.Pager.Entity;
using Microsoft.Practices.EnterpriseLibrary.Data;
using WHC.Hotel.Entity;
using WHC.Hotel.IDAL;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.DAL
{
	/// <summary>
	/// TeaConsumerList 的摘要说明。
	/// </summary>
	public class TeaConsumerList : BaseDAL<TeaConsumerListInfo>, ITeaConsumerList
	{
		#region 对象实例及构造函数

		public static TeaConsumerList Instance
		{
			get
			{
				return new TeaConsumerList();
			}
		}
		public TeaConsumerList() : base("CL_TeaConsumerList","ID")
		{
		}

		#endregion

		/// <summary>
		/// 将DataReader的属性值转化为实体类的属性值，返回实体类
		/// </summary>
		/// <param name="dr">有效的DataReader对象</param>
		/// <returns>实体类对象</returns>
		protected override TeaConsumerListInfo DataReaderToEntity(IDataReader dataReader)
		{
			TeaConsumerListInfo teaConsumerListInfo = new TeaConsumerListInfo();
			SmartDataReader reader = new SmartDataReader(dataReader);
			
			teaConsumerListInfo.ID = reader.GetInt32("ID");
			teaConsumerListInfo.Order_ID = reader.GetInt32("Order_ID");
			teaConsumerListInfo.RoomNo = reader.GetString("RoomNo");
            teaConsumerListInfo.ItemType = reader.GetString("ItemType");
            teaConsumerListInfo.ItemNo = reader.GetString("ItemNo");
			teaConsumerListInfo.ItemName = reader.GetString("ItemName");
			teaConsumerListInfo.Price = reader.GetDecimal("Price");
			teaConsumerListInfo.Discount = reader.GetDouble("Discount");
			teaConsumerListInfo.DiscountPrice = reader.GetDecimal("DiscountPrice");
			teaConsumerListInfo.Quantity = reader.GetDouble("Quantity");
			teaConsumerListInfo.Amount = reader.GetDecimal("Amount");
			teaConsumerListInfo.BeginTime = reader.GetDateTime("BeginTime");
			teaConsumerListInfo.EndTime = reader.GetDateTime("EndTime");
			teaConsumerListInfo.Waiter = reader.GetString("Waiter");
			teaConsumerListInfo.Creator = reader.GetString("Creator");
			
			return teaConsumerListInfo;
		}

		/// <summary>
		/// 将实体对象的属性值转化为Hashtable对应的键值
		/// </summary>
		/// <param name="obj">有效的实体对象</param>
		/// <returns>包含键值映射的Hashtable</returns>
        protected override Hashtable GetHashByEntity(TeaConsumerListInfo obj)
		{
		    TeaConsumerListInfo info = obj as TeaConsumerListInfo;
			Hashtable hash = new Hashtable(); 
			
 			hash.Add("Order_ID", info.Order_ID);
 			hash.Add("RoomNo", info.RoomNo);
            hash.Add("ItemType", info.ItemType);
            hash.Add("ItemNo", info.ItemNo);
 			hash.Add("ItemName", info.ItemName);
 			hash.Add("Price", info.Price);
 			hash.Add("Discount", info.Discount);
 			hash.Add("DiscountPrice", info.DiscountPrice);
 			hash.Add("Quantity", info.Quantity);
 			hash.Add("Amount", info.Amount);
 			hash.Add("BeginTime", info.BeginTime);
 			hash.Add("EndTime", info.EndTime);
 			hash.Add("Waiter", info.Waiter);
 			hash.Add("Creator", info.Creator);
 				
			return hash;
		}
        
        /// <summary>
        /// 获取该订单的相关消费记录
        /// </summary>
        /// <param name="roomNo"></param>
        /// <returns></returns>
        public List<TeaConsumerListInfo> GetGoodsConsumption(int orderId)
        {
            string sql = string.Format(@"Select * from {0} Where Order_ID ={1}", tableName, orderId);
            TeaConsumerListInfo entity = null;
            List<TeaConsumerListInfo> list = new List<TeaConsumerListInfo>();

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(sql);
            using (IDataReader dr = db.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entity = DataReaderToEntity(dr);
                    list.Add(entity);
                }
            }
            return list;
        }


        /// <summary>
        /// 获取订单对应的商品金额（包含买断消费金额）
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public decimal GetGoodsAmount(int orderID)
        {
            string sql = string.Format("select IsNull(sum(Amount),0) from {0} where Order_ID={1}", tableName, orderID);
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(sql);
            return Convert.ToDecimal(db.ExecuteScalar(command));
        }
    }
}